class JobWrapper(object):
    def __init__(self, before=None,after=None,failed=None):
        self.before = before
        self.after = after
        self.failed = failed

    def __call__(self, func):  # 接受函数
        def wrapper(*args, **kwargs):
            if self.before:
                self.before()
            try:
                func(*args, **kwargs)
                if self.after:
                    self.after()
            except Exception as e:
                if self.failed:
                    self.failed(e)
                #raise e

        return wrapper  # 返回函数

# def before(name):
#     print("before"+name)
#
# def after():
#     print("after")
#
# def failed(err):
#     print("failed"+str(err))
#
# @JobWrapper(before=before("lll"),after=after,failed=failed)
# def say(something):
#     a = 1/0
#     print("say {}!".format(something))
# say('sss')
# class JobWrapper(object):

    # @classmethod
    # def wrap(self,func,before=None,after=None,failed=None):
    #     def inner(*args):
    #         if before:
    #             if failed is None:
    #                 if after is None:
    #                     before_args = args[len(args)-1:]
    #                     func_args = args[:len(args)-1]
    #                 else:
    #                     before_args = args[len(args)-2:len(args)-1]
    #                     func_args = args[:len(args) - 2]
    #             else:
    #                 if after is None:
    #                     before_args = args[len(args)-2:len(args)-1]
    #                     func_args = args[:len(args) - 2]
    #                 else:
    #                     before_args = args[len(args)-3:len(args)-2]
    #                     func_args = args[:len(args) - 3]
    #             before(before_args)
    #
    #         else:
    #             if failed is None:
    #                 if after is None:
    #                     func_args = args
    #                 else:
    #                     func_args = args[:len(args) - 1]
    #             else:
    #                 if after is None:
    #                     func_args = args[:len(args) - 1]
    #                 else:
    #                     func_args = args[:len(args) - 2]
    #         try:
    #             func(*func_args)
    #
    #             if after:
    #                 after_args = args[len(args)-1:] if failed is None else args[len(args)-2:len(args)-1]
    #                 after(after_args)
    #            # a = 4 / 0
    #         except Exception as err:
    #             if failed:
    #                 failed_args = args[len(args)-1:]
    #                 failed(failed_args)
    #     return inner

# def func_t(a,b):
#     print(a+"===="+b)
# def before(arg):
#     print("before"+str(arg))
# def after(arg):
#     print("after"+str(arg))
# def failed(arg):
#     print("failed"+str(arg))
# c = JobWrapper.wrap(func=func_t,before=before,after=after,failed=failed)
# c("1","2",[1,2,3],[4,5,6],[7,8,9])